import sqlite3
from datetime import date
from utils.config_manager import config

def create_fl_cur_log_table(db_path="robot_zl.db"):
    # 使用config获取数据库路径
    db_path =  config.get_db_path()
    """创建 fl_cur_log 表"""
    with sqlite3.connect(db_path) as conn:
        cursor = conn.cursor()
        cursor.execute('''
            CREATE TABLE IF NOT EXISTS fl_cur_log (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                userId INTEGER NOT NULL,
                create_time TEXT NOT NULL DEFAULT (DATETIME('now', 'localtime')),
                cur_date TEXT NOT NULL DEFAULT (DATE('now', 'localtime')),
                cus_id TEXT NOT NULL,
                name TEXT NOT NULL,
                res_content TEXT NOT NULL
            )
        ''')
        conn.commit()

def get_today_date():
    """获取当前日期，格式如 2025-03-06"""
    return date.today().strftime("%Y-%m-%d")

def insert_fl_cur_log(cus_id: str, name: str, res_content: str,user_id: int=1, db_path="robot_zl.db"):
    """插入一条记录到 fl_cur_log，返回新记录的ID"""
    db_path =  config.get_db_path()

    with sqlite3.connect(db_path) as conn:
        cursor = conn.cursor()
        cursor.execute('''
            INSERT INTO fl_cur_log (userId, cus_id, name, res_content)
            VALUES (?, ?, ?, ?)
        ''', (user_id, cus_id, name, res_content))
        conn.commit()
        return cursor.lastrowid

def get_fl_cur_log_today(user_id: int=1, db_path="robot_zl.db"):
    """获取当天所有 fl_cur_log 记录"""
    # 使用config获取数据库路径
    db_path =  config.get_db_path()
    today = get_today_date()
    with sqlite3.connect(db_path) as conn:
        cursor = conn.cursor()
        cursor.execute('''
            SELECT cus_id FROM fl_cur_log 
            WHERE cur_date = ? AND userId = ?
        ''', (today,user_id))
        return cursor.fetchall()


def get_data_list(user_id: int=1, db_path="robot_zl.db"):
    # 使用config获取数据库路径
    db_path =  config.get_db_path()
    today = get_today_date()
    print(today)
    with sqlite3.connect(db_path) as conn:
        cursor = conn.cursor()
        cursor.execute('''
               SELECT cus_id FROM fl_cur_log 
               WHERE cur_date = ? AND userId = ?
           ''', (today, user_id))
        return [item[0] for item in cursor.fetchall()]



def get_fl_cur_log_by_id(log_id: int, db_path="resume.db"):
    """根据ID获取单条 fl_cur_log 记录"""
    # 使用config获取数据库路径
    db_path =  config.get_db_path()
    with sqlite3.connect(db_path) as conn:
        cursor = conn.cursor()
        cursor.execute('''
            SELECT * FROM fl_cur_log 
            WHERE id = ?
        ''', (log_id,))
        return cursor.fetchone()

def get_user_fl_cur_logs(user_id: int, db_path="resume.db") -> list:
    """获取指定用户的所有 fl_cur_log 记录"""
    # 使用config获取数据库路径
    db_path =  config.get_db_path()
    with sqlite3.connect(db_path) as conn:
        cursor = conn.cursor()
        cursor.execute('''
            SELECT * FROM fl_cur_log 
            WHERE userId = ?
        ''', (user_id,))
        return cursor.fetchall()



create_fl_cur_log_table()
# 使用示例
if __name__ == "__main__":
    # 创建表
    create_fl_cur_log_table()
    # insert_fl_cur_log("1234567890", "John Doe", "Hello, World!")
    # insert_fl_cur_log("1234567891", "John Doe", "Hello, World!")
    a = get_data_list()
    print(a)